const path = require('path')
const FileManagerPlugin = require('filemanager-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const name = 'Vue ts'
const config = require('./config/projectsConfig.ts') // 引入子系统运行打包配置
let projectName = process.env.PROJECT_NAME
function resolve(dir) {
  return path.join(__dirname, dir)
}
console.log(process.env.PROJECT_NAME)

module.exports = {
  ...config[projectName],
  publicPath: './', // TODO: Remember to change this to fit your need
  lintOnSave: false,
  // 输出文件目录
  outputDir: 'dist/' + projectName + '/',
  // 放置生成的静态资源 (js、css、img、fonts) 的 (相对于 outputDir 的) 目录。
  assetsDir: 'static',
  productionSourceMap: false,
  css: {
    sourceMap: process.env.NODE_ENV !== 'production'
  },
  pluginOptions: {
    'style-resources-loader': {
      preProcessor: 'scss',
      patterns: [
        // resolve('src/styles/_variables.scss'),
        resolve('src/styles/_mixins.scss')
      ]
    }
  },

  configureWebpack: config => {
    config.plugins.push(
      new HtmlWebpackPlugin({
        filename: 'index.html',
        template: 'public/index.html',
        inject: true,
        dll: './config.js?v=' + (new Date().getTime()),
        title: ''
      })
    )
    config.plugins.push(
      // 每个应用public文件夹 不参与打包，下面文件直接copy到对应应用下
      new CopyWebpackPlugin(
        [
          {
            from: resolve('src/projects/' + projectName + '/public'),
            to: resolve('dist/' + projectName)
          }
        ]
      )
    )
    if (process.env.NODE_ENV === 'production') {
      // 为生产环境修改配置...
      config.plugins.push(
        new FileManagerPlugin({
          onEnd: {
          // 删除压缩包
            delete: [
              'dist/' + projectName + '.zip'
            ],
            // 压缩
            archive: [
              { source: 'dist/' + projectName, destination: 'dist/' + projectName + '.zip' }
            ]
          }
        })

      )
    } else {
      // 为开发环境修改配置...
    }
  },
  chainWebpack(config) {
    // config.plugins.delete('preload')
    // name
    config.set('name', name)

    // 别名
    config.resolve.alias
      .set('@demo', resolve('src/projects/demo'))

    // when there are many pages, it will cause too many meaningless requests
    config.plugins.delete('prefetch')

    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          // svg  字体放static/fonts下面
          config.module
            .rule('svg')
            .exclude.add(resolve('src/icons')) // 排除svg图标
            .end()
            .use('file-loader')
            .loader('file-loader')
            .tap(options => {
              options.name = 'static/fonts/[name].[hash:8].[ext]'
              return options
            })
          config
            .optimization.splitChunks({
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial' // only package third parties that are initially dependent
                },
                elementUI: {
                  name: 'chunk-elementUI',
                  priority: 20, // 权限要比libs大   不然打到libs里
                  test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                },
                commons: {
                  name: 'chunk-commons',
                  test: path.resolve(__dirname, 'src/components'),
                  minChunks: 3, //  minimum common number
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
          config.optimization.runtimeChunk('single')
        }
      )
  }
}
